package com.ourteam.product.api;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.SystemUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.product.ui.ProductUIConfigFactory;
import com.ourteam.product.ui.dao.ProductUITreeDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUITreeBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/UI/export")
public class ExportUIDataFileAPI {
	
	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@RequestMapping("/exportUIActionFile/{uiDomainId}")
	@ResponseBody
	public String exportUIActionFile(@PathVariable long uiDomainId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return ProductUIConfigFactory.loadActionConfig(uiDomainId);

	}

	@RequestMapping("/exportUIListFile/{uiDomainId}")
	@ResponseBody
	public String exportUIListFile(@PathVariable long uiDomainId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return ProductUIConfigFactory.loadListConfig(uiDomainId);

	}

	@RequestMapping("/exportUIFormFile/{uiDomainId}")
	@ResponseBody
	public String exportUIFormFile(@PathVariable long uiDomainId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return ProductUIConfigFactory.loadFormConfig(uiDomainId);

	}

	@RequestMapping("/exportUITableFile/{uiDomainId}")
	@ResponseBody
	public String exportUITableFile(@PathVariable long uiDomainId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return ProductUIConfigFactory.loadTableConfig(uiDomainId);

	}

	@RequestMapping("/exportUITabFile/{uiDomainId}")
	@ResponseBody
	public String exportUITabFile(@PathVariable long uiDomainId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return ProductUIConfigFactory.loadTabConfig(uiDomainId);

	}
	
	@RequestMapping("/exportUITreeFile/{uiDomainId}")
	@ResponseBody
	public String exportUITreeFile(@PathVariable long uiDomainId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ProductUITreeDAOQueryBean queryBean = new ProductUITreeDAOQueryBean();
		
		queryBean.createCriteria().andDomainIdEqualTo(uiDomainId);
		
		ProductUITreeBean[] treeBeans = PRODUCT_UI_SERVICE.queryProductUITrees(queryBean);
		
		StringBuffer stringBuffer = new StringBuffer();
		
		for (int i = 0; i < treeBeans.length; i++) {
			ProductUITreeBean productUITreeBean = treeBeans[i];
			
			stringBuffer.append(ProductUIConfigFactory.loadTreeConfig(productUITreeBean.getId()));
			
			stringBuffer.append(System.lineSeparator());
		}

		return stringBuffer.toString();

	}

}
